package com.ksc.storekeeper.client.view.components;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasCaption;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.ksc.storekeeper.client.view.validation.ValidatingTelecom;
import com.ksc.storekeeper.client.view.validation.Validation;

public class FormItemTelecomBox extends Composite implements HasCaption {

	private static FormItemTelecomBoxUiBinder uiBinder = GWT.create(FormItemTelecomBoxUiBinder.class);

	interface FormItemTelecomBoxUiBinder extends UiBinder<Widget, FormItemTelecomBox> {
	}

	@UiField
	FlowPanel panel;
	@UiField
	Label label;
	@UiField
	ValidatingTelecom input;
	@UiField
	InlineHTML error;

	//private int minLength;
	//private int maxLength;
	private boolean validation = false;
	private boolean required = false;

	public FormItemTelecomBox() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public String getCaption() {
		return label.getText();
	}

	@Override
	public void setCaption(String caption) {
		label.setText(caption.trim());
	}

	public boolean validateAsContactNumber() {
		boolean valid = true;
		if (validation) {
			input.setRequired(required);
			valid = input.validateContactNumber().equals(Validation.VALID) && valid;
			error.setText(input.getErrorMessage());
		}
		return valid;
	}

	public boolean validateAsAdslUsername() {
		boolean valid = true;
		if (validation) {
			input.setRequired(required);
			valid = input.validateAdslUsername().equals(Validation.VALID) && valid;
			error.setText(input.getErrorMessage());
		}
		return valid;
	}

	public void setRequired(boolean value) {
		required = value;
	}

	public boolean isValidation() {
		return validation;
	}

	public void setValidation(boolean validation) {
		this.validation = validation;
	}

	public boolean isRequired() {
		return required;
	}

	/**
	 * Get item value
	 * 
	 * @return
	 */
	public String getText() {
		return input.getText().trim();
	}

	/**
	 * Set value to item
	 * 
	 * @param text
	 */
	public void setText(String text) {
		input.setText(text.trim());
	}

	public String getErrorText() {
		return error.getText().trim();
	}

	public void setErrorText(String text) {
		error.setText(text.trim());
	}

	/**
	 * Set parameter maxLength value
	 * 
	 * @param value
	 */
	public void setMaximumLength(int length) {
		input.setMaxLength(length);
	}

	/**
	 * Set parameter minLength value
	 * 
	 * @param value
	 */
	public void setMinimumLength(int length) {
		//minLength = length;
	}

	/**
	 * 
	 * @param style
	 */
	public void setPanelStyle(String style) {
		panel.setStyleName(style);
	}

	/**
	 * 
	 * @param style
	 */
	public void setItemStyle(String style) {
		input.setStyleName(style);
	}

	public void setErrorStyle(String style) {
		error.setStyleName(style);
	}

	public void setLabelStyle(String style) {
		label.setStyleName(style);
	}

	public void clear() {
		error.setText("");
		setText("");
		input.removeInvalid();
	}
}
